<!DOCTYPE html>
<html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.ui.media.Picasa</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.events');
  goog.require('goog.log');
  goog.require('goog.testing.LooseMock');
  goog.require('goog.testing.jsunit');
  goog.require('goog.ui.Component');
  goog.require('goog.ui.Control');
  goog.require('goog.ui.ControlRenderer');
  goog.require('goog.ui.media.FlashObject');
  goog.require('goog.ui.media.PicasaAlbum');
  goog.require('goog.ui.media.PicasaAlbumModel');
</script>
</head>
<body>
<script>

  var picasa;
  var control;
  var PICASA_USERNAME = 'username';
  var PICASA_ALBUM = 'albumname';
  var PICASA_URL = 'http://picasaweb.google.com/' + PICASA_USERNAME + '/' +
      PICASA_ALBUM;
  var parent = goog.dom.createElement('div');

  function setUp() {
    picasa = new goog.ui.media.PicasaAlbum();
    var model = new goog.ui.media.PicasaAlbumModel(PICASA_USERNAME,
        PICASA_ALBUM, null, 'album title');
    control = new goog.ui.media.Media(model, picasa);
    control.setSelected(true);
  }

  function tearDown() {
    control.dispose();
  }

  function testBasicRendering() {
    control.render(parent);
    var el = goog.dom.getElementsByTagNameAndClass(
        'div', goog.ui.media.PicasaAlbum.CSS_CLASS, parent);
    assertEquals(1, el.length);
    assertEquals(PICASA_URL, control.getDataModel().getUrl());
  }

  function testParsingUrl() {
    assertExtractsCorrectly(PICASA_USERNAME, PICASA_ALBUM, null, PICASA_URL);
    assertExtractsCorrectly('foo', 'bar', null,
        'https://picasaweb.google.com/foo/bar');
    assertExtractsCorrectly('foo', 'bar', null,
        'https://www.picasaweb.google.com/foo/bar');
    assertExtractsCorrectly('foo', 'bar', null,
        'https://www.picasaweb.com/foo/bar');
    assertExtractsCorrectly('foo', 'bar', '8Hzg1CUUAZM',
        'https://www.picasaweb.com/foo/bar?authkey=8Hzg1CUUAZM#');
    assertExtractsCorrectly('foo', 'bar', '8Hzg1CUUAZM',
        'https://www.picasaweb.com/foo/bar?foo=bar&authkey=8Hzg1CUUAZM#');
    assertExtractsCorrectly('foo', 'bar', '8Hzg1CUUAZM',
        'https://www.picasaweb.com/foo/bar?foo=bar&authkey=8Hzg1CUUAZM&' +
            'hello=world#');

    var invalidUrl = 'http://invalidUrl/watch?v=dMH0bHeiRNg';
    var e = assertThrows('parser expects a well formed URL', function() {
      goog.ui.media.PicasaAlbumModel.newInstance(invalidUrl);
    });
    assertEquals(
        'failed to parse user and album from picasa url: ' + invalidUrl,
        e.message);
  }

  function testBuildingUrl() {
    assertEquals(PICASA_URL,
        goog.ui.media.PicasaAlbumModel.buildUrl(PICASA_USERNAME, PICASA_ALBUM));
  }

  function testCreatingModel() {
    var model = new goog.ui.media.PicasaAlbumModel(
        PICASA_USERNAME, PICASA_ALBUM);
    assertEquals(PICASA_USERNAME, model.getUserId());
    assertEquals(PICASA_ALBUM, model.getAlbumId());
    assertEquals(PICASA_URL, model.getUrl());
    assertUndefined(model.getCaption());
  }

  function testCreatingDomOnInitialState() {
    control.render(parent);
    var caption = goog.dom.getElementsByTagNameAndClass(
        'div',
        goog.ui.media.PicasaAlbum.CSS_CLASS + '-caption',
        parent);
    assertEquals(1, caption.length);

    var flash = goog.dom.getElementsByTagNameAndClass(
        'div', goog.ui.media.FlashObject.CSS_CLASS, parent);
    assertEquals(1, flash.length);
  }

  function assertExtractsCorrectly(
      expectedUserId, expectedAlbumId, expectedAuthKey, url) {
    var model = goog.ui.media.PicasaAlbumModel.newInstance(url);
    assertEquals('User for ' + url, expectedUserId, model.getUserId());
    assertEquals('Album for ' + url, expectedAlbumId, model.getAlbumId());
    assertEquals('AuthKey for ' + url, expectedAuthKey, model.getAuthKey());
  }
</script>
</body>
</html>
